import { ConfigEnv, UserConfigExport, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import IconResolver from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'

// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfigExport => {
  // 获取环境下对于的变量
  let env = loadEnv(mode, process.cwd(), '')
  return {
    server: {
      host: '0.0.0.0',
      proxy: {
        [env.VITE_APP_BASE_API + "/admin/acl"]: {
          target: env.VITE_SERVER,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), '')
        }, [env.VITE_APP_BASE_API + "/admin"]: {
          target: env.VITE_ADMIN_SERVER,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), '')
        },
      }
    },
    plugins: [
      vue(),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // Specify symbolId format
        symbolId: 'icon-[dir]-[name]',
      }),

      AutoImport({
        imports: ['vue'],
        resolvers: [
          ElementPlusResolver(),
          IconResolver({
            prefix: 'Icon',
          })
        ],
        dts: path.resolve(process.cwd(), 'auto-imports.d.ts'),
        eslintrc: {
          enabled: true,
        },
      }),
      Components({
        dirs: ['src/views/screen/components', 'src/components'],
        dts: path.resolve(process.cwd(), 'components.d.ts'),
        resolvers: [
          ElementPlusResolver(),
          IconResolver({
            enabledCollections: ['ep']
          })
        ],
      }),
      Icons({
        autoInstall: true,
      }),
      ElementPlus({}),
      VueSetupExtend()
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src")
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },

  }
}

